import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, Subject, firstValueFrom, of } from 'rxjs';
import { COMMON_FUNCS, DICT_QUERY, GET_PDF, GET_USER_INFO_BY_CODE, NAV_DATA, PDF_ID, PRIVACY_POLICIES, PUBLIC_KEY, USAGE_CODE_VALID, USER_AGREEMENT, VERIFY_CODE } from 'src/app/api/common';
import { BreadcrumbData } from 'src/app/components/my-breadcrumb/my-breadcrumb.component';
import { MessageService } from '../message.service';
import { COMMON_FUNCS_DATA, NAV_DATA_DATA } from 'src/assets/mock/common/common';

@Injectable({
  providedIn: 'root'
})
export class CommonService {

  constructor(
    private http: HttpClient,
    private message: MessageService
  ) { }
  isHome: Subject<boolean> = new Subject()
  breadcrumbData: Subject<BreadcrumbData[]> = new Subject()

  public rightInfoVisible: Subject<any> = new Subject()
  setRightInfoVisible(flag: boolean){
    setTimeout(() => {
      this.rightInfoVisible.next(flag)
    }, 0);
  }

  _getNavList(): Observable<any>{
    return this.http.get(NAV_DATA)
  }

  // 获取验证码
  _getVerifyCode(phone: string): Observable<any>{
    return this.http.get(VERIFY_CODE + phone)
  }

  // 获取密码加密公钥
  _getPublicKey(): Observable<any>{
    return this.http.get(PUBLIC_KEY)
  }

  _getCommonFuncs(): Observable<any>{
    return this.http.get(COMMON_FUNCS)
    // return of(COMMON_FUNCS_DATA)
  }

  _getPDF(data: any): Observable<any>{
    return this.http.post(GET_PDF, data, { responseType: 'blob' })
  }
  _getPDFId(data: any): Observable<any>{
    return this.http.post(PDF_ID, data)
  }

  // 用户协议和隐私政策
  _getUserAgreement(): Observable<any>{
    return this.http.get(USER_AGREEMENT)
  }
  _getPrivacyPolicies(): Observable<any>{
    return this.http.get(PRIVACY_POLICIES)
  }

  // 校验用户编号
  async usageCodeValid(typeId: string, usageCode: string){
    try{
      const res: any = await firstValueFrom(this.http.get(USAGE_CODE_VALID + typeId + '/' + usageCode))
      if(res && res.result === true){
        return ''
      }else{
        return res.message
      }
    }catch(error){
      return '用户编号校验失败，请重试'
    }
  }

  dictQuery(type: string): Observable<any>{
    return this.http.get(DICT_QUERY + type)
  }

  // 根据粤省事 code 获取用户姓名和身份证号
  getCardNoByCode(code: string): Observable<any>{
    return this.http.get(GET_USER_INFO_BY_CODE + code)
  }
}
